package com.abc.base.service.impl;

import com.abc.base.dto.HouseQuery;
import com.abc.base.mapper.HouseMapper;
import com.abc.base.model.HouseInfoNew;
import com.abc.base.service.HouseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, HouseInfoNew> implements HouseService {
    @Override
    public Page<HouseInfoNew> page(Page<HouseInfoNew> page, HouseQuery query) {
        // 使用 MyBatis-Plus 的 QueryWrapper 进行条件构造
        QueryWrapper<HouseInfoNew> queryWrapper = new QueryWrapper<>();

        if (query.getSearch() != null && !query.getSearch().isEmpty()) {
            queryWrapper.like("title", query.getSearch());
        }
        if (query.getMinPrice() != null) {
            queryWrapper.ge("total_price", query.getMinPrice());
        }
        if (query.getMaxPrice() != null) {
            queryWrapper.le("total_price", query.getMaxPrice());
        }
        if (query.getMinItemPrice() != null) {
            queryWrapper.ge("item_price", query.getMinItemPrice());
        }
        if (query.getMaxItemPrice() != null) {
            queryWrapper.le("item_price", query.getMaxItemPrice());
        }
        if (query.getYear() != null) {
            // 添加年限条件，这里假设您需要处理这些值
            switch (query.getYear()) {
                case "0-5":
                    queryWrapper.le("days_ago", 5);
                    break;
                case "5-10":
                    queryWrapper.between("days_ago", 5, 10);
                    break;
                case "10+":
                    queryWrapper.ge("days_ago", 10);
                    break;
            }
        }
        if (query.getMinFollowers() != null) {
            queryWrapper.ge("followers", query.getMinFollowers());
        }
        if (query.getMaxFollowers() != null) {
            queryWrapper.le("followers", query.getMaxFollowers());
        }

        return baseMapper.selectPage(page, queryWrapper); // 使用 MyBatis-Plus 查询
    }
}
